<template>
  <div class="app-main">
    <el-card shadow="never">
      <el-row>
        <el-form :inline="true" :model="queryForm" ref="queryForm" label-width="78px">
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="queryForm.status" clearable placeholder="请选择订单状态">
              <el-option v-for="item in orderStatusArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="支付状态" prop="payStatus">
            <el-select v-model="queryForm.payStatus" clearable placeholder="请选择支付状态">
              <el-option v-for="item in payStatusArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryData">查询</el-button>
            <el-button @click="resetForm('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button type="primary" plain icon="el-icon-plus" @click="addShow">预约</el-button>
      </el-row>
      <el-row class="table-container">
        <el-table :data="tableData" stripe size="medium" style="width: 100%" max-height="660">
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="宠物种类" prop="petId" :formatter="typeRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="服务项目" prop="psid" :formatter="serviceRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="订单金额" prop="psid" :formatter="rowPriceRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="提交时间" prop="submitTime" show-overflow-tooltip></el-table-column>
          <el-table-column label="订单状态" prop="status" :formatter="statusRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="支付状态" prop="payStatus" :formatter="payStatusRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button class="el-icon-check" :disabled="(scope.row.status ===1 && scope.row.payStatus ===1) || (scope.row.status !==1)" type="text" @click="takeOrder(scope.row)">订单支付</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNo"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-row>
    </el-card>
    <el-dialog title="服务预约" :visible.sync="addFormVisible" width="600px" :close-on-click-modal="closeModal">
      <el-form :model="addForm" :rules="rules" ref="addForm" label-width="90px">
        <el-form-item label="宠物种类" prop="petType">
          <el-select v-model="addForm.petType" placeholder="请选择宠物种类" clearable style="width: 100%">
            <el-option v-for="item in petTypeArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务项目" prop="serviceType">
          <el-select v-model="addForm.serviceType" placeholder="请选择宠物种类" clearable style="width: 100%">
            <el-option v-for="item in serviceTypeArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelForm('addForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="订单支付" :visible.sync="payVisible" width="380px" :close-on-click-modal="closeModal">
      <div>
        <img :src="payCodeImg" width="100%" alt="">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import payCode from '@/assets/images/service/payCode.png'
import {getOrder, addOrder, editOrder} from "@/api/order";
import {getServiceList} from "@/api/service";
import {mapState} from "vuex";
export default {
  name: "index",
  data() {
    return {
      petTypeArr: [
        {'label': '猫', 'value': '1'},
        {'label': '狗', 'value': '2'},
        {'label': '仓鼠', 'value': '3'},
        {'label': '小猪', 'value': '4'},
        {'label': '兔子', 'value': '5'},
        {'label': '羊驼', 'value': '6'},
        {'label': '鹦鹉', 'value': '7'},
        {'label': '鸽子', 'value': '8'},
        {'label': '乌龟', 'value': '9'},
        {'label': '蝈蝈', 'value': '10'},
        {'label': '金鱼', 'value': '11'},
        {'label': '桑蚕', 'value': '12'},
      ],
      petGenderArr: [
        {'label': '公', 'value': 1},
        {'label': '母', 'value': 2}
      ],
      vaccinateArr: [
        {'label': '是', 'value': 1},
        {'label': '否', 'value': 0}
      ],
      orderStatusArr: [
        {'label': '未接单', 'value': 0},
        {'label': '已接单', 'value': 1}
      ],
      payStatusArr: [
        {'label': '未支付', 'value': 0},
        {'label': '已支付', 'value': 1}
      ],
      serviceTypeArr: [],
      queryForm: {
        status: '',
        payStatus: ''
      },
      tableData: [],
      page: {
        pageNo: 1,
        pageSizes: [10, 50, 100, 200, 500],
        pageSize: 10,
        total: 3
      },
      payVisible: false,
      closeModal: false,
      payCodeImg: payCode,
      addFormVisible: false,
      addForm: {
        petType: '',
        serviceType: '',
        serviceDesc: ''
      },
      rules: {
        petType: [
          {required: true, message: '请选择宠物种类', trigger: 'change'},
        ],
        serviceType: [
          {required: true, message: '请选择服务项目', trigger: 'change'},
        ],
      },
    }
  },
  computed: {
    ...mapState({
      nickname: state => state.user.nickname
    })
  },
  created() {
    this.queryData()
    this.queryPetType()
  },
  methods: {
    typeRender(data) {
      for(let i=0; i<this.petTypeArr.length; i++) {
        if(data.petId === Number(this.petTypeArr[i].value)) {
          return this.petTypeArr[i].label
        }
      }
    },
    statusRender(data) {
      for(let i=0; i<this.orderStatusArr.length; i++) {
        if(data.status === Number(this.orderStatusArr[i].value)) {
          return this.orderStatusArr[i].label
        }
      }
    },
    payStatusRender(data) {
      for(let i=0; i<this.orderStatusArr.length; i++) {
        if(data.payStatus === Number(this.payStatusArr[i].value)) {
          return this.payStatusArr[i].label
        }
      }
    },
    serviceRender(data) {
      for(let i=0; i<this.serviceTypeArr.length; i++) {
        if(data.psid === this.serviceTypeArr[i].id ) {
          return this.serviceTypeArr[i].name
        }
      }
    },
    rowPriceRender(data) {
      for(let i=0; i<this.serviceTypeArr.length; i++) {
        if(data.psid === this.serviceTypeArr[i].id ) {
          return this.serviceTypeArr[i].price
        }
      }
    },
    queryPetType() {
      let params = {
        pageNo: 1,
        pageSize: 1000
      }
      getServiceList(params).then(response => {
        if(response.code === 200) {
          this.serviceTypeArr = response.data.list
        } else {
          this.$message.error(response.msg);
        }
      })
    },
    queryData() {
      this.loading = true
      let params = {
        accounts: this.nickname,
        status: this.queryForm.status,
        payStatus: this.queryForm.payStatus,
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize
      }
      getOrder(params).then(response => {
        if(response.code === 200) {
          this.tableData = response.data.list
          this.page.total = response.data.total
        } else {
          this.$message.error(response.msg);
        }
        this.loading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.queryData()
    },
    addShow() {
      this.addForm.petType = ''
      this.addForm.serviceType = ''
      this.addFormVisible = true
    },
    getPrice() {
      for(let i=0; i<this.serviceTypeArr.length; i++) {
        if(this.addForm.serviceType === this.serviceTypeArr[i].id ) {
          return this.serviceTypeArr[i].price
        }
      }
    },
    submitForm(formName) {
      const servicePrice = this.getPrice()
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            petId: this.addForm.petType,
            psid: this.addForm.serviceType,
            payStatus: '0',
            status: '0',
            price: servicePrice
          }
          // console.log(params, '订单')
          addOrder(params).then(response => {
            if(response.code === 200){
              this.$message({
                message: response.msg,
                type: 'success'
              });
            } else {
              this.$message({
                message: response.msg,
                type: 'error'
              });
            }
            this.addFormVisible = false
            this.queryData()
          })
        } else {
          return false;
        }
      });
    },
    cancelForm(formName) {
      this.$refs[formName].resetFields();
      this.addFormVisible = false
    },
    takeOrder(row) {
      this.$confirm(`此单金额为${row.price}元，是否确认付款?`).then(function () {
        return editOrder({
          id: row.id,
          payStatus: 1
        })
      }).then((response) => {
        if (response.code === 200) {
          this.payVisible = true
          setTimeout(() => {
            this.$message({
              message: response.msg,
              type: 'success'
            });
            this.queryData()
          },2000)
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消支付'
        });
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.page.pageNo = 1
      this.queryData()
    },
    handleCurrentChange(val) {
      this.page.pageNo = val
      this.queryData()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-main {
  padding: 20px;
  box-sizing: border-box;

  .table-container {
    margin-top: 20px;

    .operate-icon {
      margin-right: 5px;
    }
  }

  .pagination-container {
    margin: 20px 0;
    float: right;
  }
}
</style>
